package com.king.modules.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.annotation.Log;
import com.king.common.enums.BusinessType;
import com.king.common.utils.poi.ExcelUtil;
import com.king.framework.base.controller.YyBaseController;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.modules.system.domain.SysDictDataEntity;
import com.king.modules.system.domain.SysDictTypeEntity;
import com.king.modules.system.service.SysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author king
 */
@RestController
@RequestMapping("/system/dictType")
public class SysDictTypeController extends YyBaseController<SysDictTypeEntity> {
	
	@Autowired
	private SysDictTypeService dictTypeService;


	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	@PreAuthorize("@sps.hasAuthority('system:dict:export')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, SysDictTypeEntity dictType) {
		List<SysDictTypeEntity> list = dictTypeService.selectDictTypeList(dictType);
		ExcelUtil<SysDictTypeEntity> util = new ExcelUtil<SysDictTypeEntity>(SysDictTypeEntity.class);
		util.exportExcel(response, list, "字典类型");
	}


	/**
	 * 刷新字典缓存
	 * @throws Exception 
	 */
	@PreAuthorize("@sps.hasAuthority('system:dict:del')")
	@Log(title = "字典类型", businessType = BusinessType.CLEAN)
	@DeleteMapping("/refreshCache")
	public AjaxResult refreshCache() throws Exception {
		dictTypeService.resetDictCache();
		return success();
	}

	
	/**
	 * 获取枚举数据缓存到前端
	 * @param request
	 * @return
	 */
	@GetMapping("/getEnumData")
	public AjaxResult getEnumData(HttpServletRequest request) {
		Map<String, List<SysDictDataEntity>> datas = dictTypeService.selectDictMap();
		return success(datas);
	}
}
